﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Frame.Lib.Core.Dispatch;
using Frame.Lib.Utilities;
using Frame.Lib.Common;

namespace Frame.Web.Admin.Dispatch
{
    public partial class List : PageList
    {
        private DispatchBO Dispatch
        {
            get
            {
                return DispatchBO.Instance<DispatchBO>();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindView();
            }
            AddAttr();
            base.InitSelectID(hidSelectIDs);

            lblLastRunTime.Text = AppDispatch.Dispatch.LastRunTime.ToString();
        }

        private void AddAttr()
        {
            JSUtil.ShowForm(lnkAdd, "增加调度", null, GlobalVar.WebCurrPath + "Edit.aspx", 370, 171);
            JSUtil.ClickBtn(lnkRestart, btnRestart);
            JSUtil.ClickBtn(lnkDelete, btnDelete);
        }
        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (strSelectIDs == "")
            {
                return;
            }
            List<int> lstSelectIDs = StrUtil.SplitToInt(strSelectIDs);
            this.Dispatch.Delete(lstSelectIDs);
            BindView();
        }
        private void BindView()
        {
            LVUtil.Init(lv, lvHeader);

            List<DispatchInfo> list = this.Dispatch.GetList();
            lv.DataSource = list;
            lv.DataKeyNames = new string[] { "ID"};
            lv.DataBind();
        }

        protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                LVUtil.SetEdit(sender, e, 370, 171);
            }
        }

        protected void btnRestart_Click(object sender, EventArgs e)
        {
            AppDispatch.Dispatch.Update();
            AppDispatch.Dispatch.Start();
            JSUtil.Alert("启动成功");
            BindView();
        }

        protected void btnStop_Click(object sender, EventArgs e)
        {
            AppDispatch.Dispatch.Stop();
        }
    }
}